import {wxRequest} from '../../../utils/server.js';
import { treefun} from '../../../utils/tree.js';

const _wxRequest = new wxRequest();
const _tree = new treefun()
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    datalist: [],
    itemName: '',
    name: '',
    apiurl: ''
  },

  onLoad: function(options) {
    if (options) {
      this.setData({
        itemName: options.itemName,
        name: options.name,
        apiurl: options.apiurl,
        addUrl: options.addUrl || ''
      })
    }
    this.getlist();
  },

  // 获取列表
  getlist: function() {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/district/findProvAndCity',
      method: "post",
      data: {
        status: 1,
        pageSize: 10000
      }
    }, (res)=>{
      var list = _tree.treeData(res.list);
      that.setData({
        datalist: list
      })
      that.highlight()
    })
  },

  //选择高亮
  highlight:function(){
    var oldData = wx.getStorageSync('cityIds');
    oldData = JSON.stringify(oldData)
    console.log(oldData,'oldData')
    var datalist = this.data.datalist; 

    datalist.map((item)=>{  
      item.children.map(items=>{
        if (oldData.indexOf(items.id) > -1) { 
          items.check = true
        } 
      })   
      var flag=item.children.filter(item=>item.check==true)
      if (flag.length == item.children.length){
        item.check = true
      }
    })

    this.setData({
      datalist
    })
  },


  //监听搜索
  seacherOn: function(e) {
    var keywords = e.detail
    this.setData({
      keywords
    })
    this.getlist()
  },


  //选择
  choose: function(e) {
    var item = e.currentTarget.dataset.item
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面   
    var itemName = this.data.itemName
    prevPage.setData({
      [itemName]: item,
    })
    wx.navigateBack();
  },

  //选择省份
  chooseProv: function(e) {
    var id = e.currentTarget.dataset.id;
    var datalist = this.data.datalist;
    datalist.map(item => {
      if (item.id == id) {
        item.check = !item.check
        item.children.map(items => {
          console.log(item.check, 'item.check')
          items.check = item.check
        })
      }
    })

    this.setData({
      datalist
    })
  },



  //选择城市
  chooseCity: function(e) {
    var pid = e.currentTarget.dataset.pid;
    var id = e.currentTarget.dataset.id;
    var datalist = this.data.datalist;
    datalist.map(item => {
      if (item.id == pid) {
        item.check = false
        item.children.map(items => {
          if (items.id == id) {
            items.check = !items.check
          }
        })
      }
      //所有都选择父级高亮
      var flag = item.children.filter(item => item.check == true)
      if (flag.length == item.children.length) {
        item.check = true
      }
    })

    

    this.setData({
      datalist
    })
  },


  //重置
  reset: function() {
    var datalist = this.data.datalist;
    datalist.map(item => {
      item.check = false
      item.children.map(items => {
        items.check = false
      })
    })
    this.setData({
      datalist
    })
  },



  //确认
  confirm: function() {
    var datalist = this.data.datalist;
    var arr = []; 
    datalist.map(item => {
      item.children.map(items => {
        if (items.check == true) {
          var obj={
            disId: items.id,
            disName: items.name
          }  

          arr.push(obj)
        }
      });
    })

    if (arr.length<1){
      wx.showToast({
        icon: 'none',
        title: '请选择城市'
      })
      return
    }
     
    var cityIds = arr.map(item => item.disId)  
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面    
    prevPage.setData({
      cityItem: arr,
      cityIds
    }) 
    wx.navigateBack(); 
  },

 



})